//
//  JMOrderHeaderV.m
//  NewiOSProject
//
//  Created by JMMac on 16.1.24.
//  Copyright © 2024 范智渊. All rights reserved.
//

#import "JMOrderHeaderV.h"
#import "JMOrderInfoCell.h"

@implementation JMOrderHeaderV
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        _headView = [[UIView alloc]initWithFrame:(CGRectMake(0, 0, kAppW(), 314))];
        [self addSubview:_headView];
        
        _bgImgV = [[UIImageView alloc]initWithFrame:(CGRectMake(0, 0, kAppW(), 150))];
        [_headView addSubview:_bgImgV];
        [_bgImgV setRoundedCorners:(UIRectCornerBottomLeft |UIRectCornerBottomRight) radii:60];
        _bgImgV.backgroundColor = Hex_Str_COLOR(@"#3C57F8");
        self.backgroundColor = WhiteColor();

        
        _stateTitleLab = [JMControl createLabelWithFrame:(CGRectMake(24, +16, kAppW(), 20)) Font:Font(12) Text:@"Estado del pedido" color:WhiteColor()];
        [_headView addSubview:_stateTitleLab];
       
        _stateLab = [JMControl createLabelWithFrame:(CGRectMake(24, _stateTitleLab.bottom+16, kAppW(), 31)) Font:mediumFont(22) Text:@"" color:WhiteColor()];
        [_headView addSubview:_stateLab];
        
        _warningImgV = [[UIImageView alloc]initWithFrame:(CGRectMake(24, 0, 24, 24))];
        [_headView addSubview:_warningImgV];
        [_warningImgV setImage:ImageNamed(@"ICON／bagnz")];
        _warningImgV.centerY = _stateLab.centerY;
        _warningImgV.hidden = YES;
        
        
        _iconImgV = [[UIImageView alloc]initWithFrame:(CGRectMake(kAppW()-98, 16, 74, 74))];
        _iconImgV.cornerRadius = 2;
        [_headView addSubview:_iconImgV];
        
        _contV = [[UIView alloc]initWithFrame:(CGRectMake(24,_bgImgV.bottom-48, kAppW()-48, 146))];
        _contV.layer.cornerRadius = 8;
        _contV.layer.shadowOpacity = 0.9;
        _contV.layer.shadowColor = HexAlpha_Str_COLOR(@"#B453FF", 0.2).CGColor;
        _contV.layer.shadowOffset = CGSizeMake(0, 2);
        _contV.layer.masksToBounds = NO;
        [_headView addSubview:_contV];
        _contV.backgroundColor = GradientColorWithColor(_contV.size, FXGradientChangeDirectionVertical, HexAlpha_Str_COLOR(@"#ffffff", 0.85), HexAlpha_Str_COLOR(@"#ffffff", 1));
   
        
        _slogin = [JMControl createImageViewWithFrame:CGRectMake(13, 13, 24, 24) ImageName:nil];
        [_contV addSubview:_slogin];
        
        
        _nameLab = [JMControl createAutoWidthLabelWithOrgin:(CGPointMake(_slogin.right+12, _slogin.top)) Font:Font(15) Text:@"asdfasdfasdfasdfasdfasdf" color:Hex_Str_COLOR(@"#222222")];
        _nameLab.centerY = _slogin.centerY;
        [_contV addSubview:_nameLab];
        [JMControl addHorLineWithSpace:0 top:_slogin.bottom+12 height:1 bgColor:mainSepartorColor() toView:_contV];
        
        _showDetailBtn = [JMControl createButtonWithFrame:(CGRectMake(0, 12, 150, 24)) ImageName:@"ICON／SHANG-xiao" Target:self Action:@selector(showOrHideDetailView:) font:Font(12) Title:@"Detalles de la factura"];
        _showDetailBtn.cornerRadius =12;
        _showDetailBtn.width = CalculateLabelWidthWithLab(_showDetailBtn.titleLabel)+40;
        [_showDetailBtn TiaoZhengButtonWithOffsit:0 TextImageSite:(UIButtonTextLeft)];
        _showDetailBtn.right = _contV.width-12;
        [_showDetailBtn setTitleColor:Hex_Str_COLOR(@"#3C57F8") forState:(UIControlStateNormal)];
        _showDetailBtn.backgroundColor = Hex_Str_COLOR(@"#C5CDFD");
        [_contV addSubview:_showDetailBtn];
        _showDetailBtn.hidden = YES;
      
        
        _loanMoneyLab = [JMControl createLabelWithFrame:(CGRectMake(_slogin.left, _slogin.bottom+30, _contV.width/2, FontH(12))) Font:Font(12) Text:@"Monto del préstamo($)" color:Hex_Str_COLOR(@"#666666")];
        [_contV addSubview:_loanMoneyLab];
        
        _loanMoneyNumLab = [JMControl createLabelWithFrame:(CGRectMake(_loanMoneyLab.right, _loanMoneyLab.bottom+10, _contV.width/2, FontH(23))) Font:mediumFont(22) Text:@"" color:Hex_Str_COLOR(@"#3C57F8")];
        [_contV addSubview:_loanMoneyNumLab];
        _loanMoneyNumLab.centerY = _loanMoneyLab.centerY;
        
        
        _termLab = [JMControl createLabelWithFrame:(CGRectMake(_loanMoneyLab.left, _loanMoneyLab.bottom+26, _contV.width/2, FontH(12))) Font:Font(12) Text:@"Plazo del préstamo(Días)" color:Hex_Str_COLOR(@"#666666")];
        [_contV addSubview:_termLab];
        
        _termNumLab = [JMControl createLabelWithFrame:(CGRectMake(_termLab.right, _loanMoneyLab.bottom+10, _contV.width/2, _loanMoneyNumLab.height)) Font:_loanMoneyNumLab.font Text:@"" color:Hex_Str_COLOR(@"#3C57F8")];
        [_contV addSubview:_termNumLab];
        _termNumLab.centerY = _termLab.centerY;
        
        
        _soloLab = [JMControl createLabelWithFrame:(CGRectMake(0, _contV.bottom+24, 150, 24)) Font:fontWithWeight(17, UIFontWeightMedium) Text:@"Detalles del préstamo" color:Hex_Str_COLOR(@"#222222")];
        [_headView addSubview:_soloLab];
        _soloLab.backgroundColor = WhiteColor();
        _soloLab.textAlignment = 1;
        _soloLab.centerX = kAppW()/2;
        
        _lineV = [[UIView alloc]initWithFrame:(CGRectMake(0, 0, 198, 2))];
        _lineV.backgroundColor = Hex_Str_COLOR(@"#3C57F8");
        [_headView addSubview:_lineV];
        [_headView sendSubviewToBack:_lineV];
        _lineV.center = _soloLab.center;
                
        CGFloat w = CalculateLabelWidthWithLab(_soloLab);
        _soloLab.width = w;
        _soloLab.centerX = kAppW()/2;
        _lineV.width = w+16+48;
        _lineV.centerX = _soloLab.centerX;
        
        _headView.height = _soloLab.bottom+16;
        self.height = _headView.bottom;
        
        
        _tipsView = [JMControl createLabelWithFrame:(CGRectMake(24, _contV.bottom+24, kAppW()-48, 75)) Font:Font(12) Text:@"Los métodos de pago solo incluyen STP, Transfer y OXXO, otras cuentas bancarias no son válidas, preste especial atención" color:Text_COLOR_LEVEL2()];
        [_headView addSubview:_tipsView];
        [JMControl changeTextColorWithLabel:_tipsView changeStr:@"STP, Transfer y OXXO, " color:Hex_Str_COLOR(@"#EC533E")];
        _tipsView.hidden = YES;
        
    }
    return self;
}
-(void)setInfoModel:(JMOrderInfoModel *)infoModel{
    _infoModel = infoModel;
    
    if (infoModel.status.intValue == 180) {
        _bgImgV.backgroundColor = Hex_Str_COLOR(@"#BD4232");
        _warningImgV.hidden = NO;
        _stateLab.left = 52;
    }
    _slogin.image = infoModel.iconImg;
    _nameLab.text = infoModel.productName?:@"";
    _stateLab.text = infoModel.statusString;
    [_iconImgV setImage:ImageNamed(infoModel.statusImgName)];
    _loanMoneyNumLab.text = infoModel.loanAmount;
    _termNumLab.text = infoModel.term?infoModel.term.stringValue:@"0";
    
    
}
-(void)setPaymentModel:(JMRepaymentModel *)paymentModel{
    _paymentModel = paymentModel;
    if (paymentModel.extModel) {
        self.extinfoModel = paymentModel.extModel;
        [_iconImgV setImage:ImageNamed(@"fen qi huan kuan")];
        
    }else{
        self.infoModel = paymentModel.infoModel;
        [_iconImgV setImage:ImageNamed(@"huan kuan")];
        _showDetailBtn.hidden = NO;
        [self addSubview:self.detailView];
        self.isFold = YES;
        _stateLab.text = @"Reembolsado";
        _loanMoneyLab.text = @"Monto de pago($）";
        _loanMoneyNumLab.text = paymentModel.amount.stringValue.moneyString;
        _termLab.text = @"Fecha de vencimiento";
        _termNumLab.text = paymentModel.infoModel.expiryTime;
    }

}
-(void)setExtinfoModel:(JMExtenDetailModel *)extinfoModel{
    _extinfoModel = extinfoModel;
    if (extinfoModel.infoModel.status.intValue == 180) {
        _bgImgV.backgroundColor = Hex_Str_COLOR(@"#BD4232");
        _warningImgV.hidden = NO;
        _stateLab.left = 52;
    }
    _slogin.image = extinfoModel.infoModel.iconImg;
    _nameLab.text = extinfoModel.infoModel.productName?:@"";
    _stateLab.text = @"Prórroga de plazo";

    [_iconImgV setImage:ImageNamed(@"ding dan ming xi")];
    _loanMoneyNumLab.text = extinfoModel.amount;
    _termNumLab.text = extinfoModel.delayExpiryTime;
    
    _loanMoneyLab.text = @"Gasto de prórrog{$）";
    _termLab.text = @"Nueva fecha de vencimiento";

}

-(UIView *)detailView{
    if (!_detailView) {
        _detailView = [[UIView alloc]initWithFrame:(CGRectMake(0, _contV.bottom+24, kAppW(), 310))];
    
        for (int i=0; i<self.infoModel.pairs.count; i++) {
            JMOrderInfoCell *cell = [[JMOrderInfoCell alloc]initWithFrame:(CGRectMake(0, 20+52*i + 52*i, kAppW(), 52))];
            cell.top =20+52*i;
            NSDictionary*dict = self.infoModel.pairs[i];
            cell.model = dict;
            [_detailView addSubview:cell];
        }
        
        UIButton *foldBtn = [JMControl createButtonWithFrame:(CGRectMake(0, 20+52*self.infoModel.pairs.count, kAppW(), 52)) ImageName:@"ICON／XIA-xiao" Target:self Action:@selector(showOrHideDetailView:) font:Font(12) Title:@"Ocultar"];
        [foldBtn setTitleColor:Hex_Str_COLOR(@"#3C57F8") forState:(UIControlStateNormal)];
        [_detailView addSubview:foldBtn];
        foldBtn.tag = 1;
        [foldBtn TiaoZhengButtonWithOffsit:2 TextImageSite:(UIButtonTextLeft)];
        
        [JMControl addHorLineWithSpace:0 top:foldBtn.bottom height:12 bgColor:Hex_Str_COLOR(@"#F6F9FE") toView:_detailView];
        _detailView.height = foldBtn.bottom+12;
    }
    return _detailView;
}
-(void)showOrHideDetailView:(UIButton *)sender{
    self.isFold = sender.tag;
}
-(void)setIsFold:(BOOL)isFold{
    _isFold = isFold;
    if (isFold) {
        _showDetailBtn.hidden = NO;
        _detailView.hidden = YES;
        self.height = _tipsView.bottom;
        
        _tipsView.hidden = NO;
        _soloLab.hidden = YES;
        _lineV.hidden = YES;
        self.height = self.tipsView.bottom+20;
    }else{
        _tipsView.hidden = YES;
        _soloLab.hidden = NO;
        _lineV.hidden = NO;
        
        _showDetailBtn.hidden = YES;
        _detailView.hidden = NO;
        self.height = _detailView.bottom;
    }
    if([self.delegate respondsToSelector:@selector(updateHeight)]) {
        [self.delegate updateHeight];
    }
}

@end
